using System;
using System.Collections.Generic;

namespace SeataNet.Core.Saga
{
    /// <summary>
    /// Saga事务状态
    /// </summary>
    public enum SagaTransactionStatus
    {
        /// <summary>
        /// 已开始
        /// </summary>
        Started,

        /// <summary>
        /// 运行中
        /// </summary>
        Running,

        /// <summary>
        /// 活跃状态
        /// </summary>
        Active,

        /// <summary>
        /// 成功
        /// </summary>
        Succeeded,

        /// <summary>
        /// 已提交
        /// </summary>
        Committed,

        /// <summary>
        /// 已补偿
        /// </summary>
        Compensated,

        /// <summary>
        /// 已回滚
        /// </summary>
        RolledBack,

        /// <summary>
        /// 失败
        /// </summary>
        Failed,

        /// <summary>
        /// 超时
        /// </summary>
        Timeout
    }

    /// <summary>
    /// Saga步骤状态
    /// </summary>
    public enum SagaStepStatus
    {
        /// <summary>
        /// 已注册
        /// </summary>
        Registered,

        /// <summary>
        /// 活跃状态
        /// </summary>
        Active,

        /// <summary>
        /// 执行中
        /// </summary>
        Running,

        /// <summary>
        /// 已完成
        /// </summary>
        Completed,

        /// <summary>
        /// 已补偿
        /// </summary>
        Compensated,

        /// <summary>
        /// 补偿失败
        /// </summary>
        CompensationFailed,

        /// <summary>
        /// 失败
        /// </summary>
        Failed
    }

    /// <summary>
    /// Saga事务上下文
    /// </summary>
    public class SagaTransactionContext
    {
        /// <summary>
        /// 事务XID
        /// </summary>
        public string Xid { get; set; } = string.Empty;
        
        /// <summary>
        /// 事务ID
        /// </summary>
        public long TransactionId { get; set; }
        
        /// <summary>
        /// 事务名称
        /// </summary>
        public string TransactionName { get; set; } = string.Empty;
        
        /// <summary>
        /// Saga名称
        /// </summary>
        public string? SagaName { get; set; }
        
        /// <summary>
        /// 事务状态
        /// </summary>
        public SagaTransactionStatus Status { get; set; }
        
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; }
        
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }
        
        /// <summary>
        /// 超时时间（毫秒）
        /// </summary>
        public int Timeout { get; set; }
        
        /// <summary>
        /// 补偿方法
        /// </summary>
        public string? CompensationMethod { get; set; }
        
        /// <summary>
        /// 重试策略
        /// </summary>
        public string? RetryPolicy { get; set; }
        
        /// <summary>
        /// Saga步骤列表
        /// </summary>
        public List<SagaStepContext> Steps { get; set; } = new List<SagaStepContext>();
    }

    /// <summary>
    /// Saga步骤上下文
    /// </summary>
    public class SagaStepContext
    {
        /// <summary>
        /// 步骤ID
        /// </summary>
        public string StepId { get; set; } = string.Empty;
        
        /// <summary>
        /// 步骤名称
        /// </summary>
        public string StepName { get; set; } = string.Empty;
        
        /// <summary>
        /// 补偿方法
        /// </summary>
        public string CompensationMethod { get; set; } = string.Empty;
        
        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; } = string.Empty;
        
        /// <summary>
        /// 步骤状态
        /// </summary>
        public SagaStepStatus Status { get; set; }
        
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedTime { get; set; }
        
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; }
        
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }
        
        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? UpdatedTime { get; set; }
        
        /// <summary>
        /// 输入参数
        /// </summary>
        public object? Input { get; set; }
        
        /// <summary>
        /// 输出结果
        /// </summary>
        public object? Output { get; set; }
        
        /// <summary>
        /// 结果
        /// </summary>
        public object? Result { get; set; }
        
        /// <summary>
        /// 补偿动作
        /// </summary>
        public Func<object?, Task>? CompensationAction { get; set; }
        
        /// <summary>
        /// 重试次数
        /// </summary>
        public int RetryCount { get; set; }
        
        /// <summary>
        /// 重试间隔（毫秒）
        /// </summary>
        public int RetryInterval { get; set; }
        
        /// <summary>
        /// 异常信息
        /// </summary>
        public Exception? Exception { get; set; }
    }
}